/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditQuestionView.java
 * Date: 04/02/2011
 */

package com.ingenius.sau.client.view;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ListField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.ingenius.sau.client.model.OptionBeanModel;
import com.ingenius.sau.client.presenter.EditQuestionPresenter.EditQuestionDisplay;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.instruments.Question;
import com.ingenius.sau.shared.model.instruments.ResponseOption;

/**
 * Class <strong>EditQuestionView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditQuestionView extends AbstractEditView implements
		EditQuestionDisplay {

	// UI Widgets
	private final TextField<String> textTitle;
	private final TextArea textContent;
	private final ListBox comboType;
	private final ListField<OptionBeanModel> listResponses;

	private final Button addButton;
	private final Button deleteButton;
	private Object selectedOption;

	private final ListStore<OptionBeanModel> store;

	// Constructor

	public EditQuestionView() {
		super();
		comboType = new ListBox();
		listResponses = new ListField<OptionBeanModel>();
		textTitle = new TextField<String>();
		textContent = new TextArea();
		addButton = new Button("Add");
		deleteButton = new Button("Delete");

		store = new ListStore<OptionBeanModel>();

		// TODO i18n
		setHeading("Edit Question");
		setSize(500, 420);
		registerHandlers();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractEditView#createComponents()
	 */
	@Override
	protected void createComponents() {
		// TODO i18n
		FormData formData = new FormData("-20");
		textTitle.setFieldLabel("Title");
		textTitle.setAllowBlank(false);
		textTitle.addPlugin(plugin);
		textTitle.setData("text", "Enter the text to identify the question");
		textTitle.getMessages().setBlankText("Title is required");

		textContent.setFieldLabel("Content");
		textContent.setHeight(75);
		textContent.setAllowBlank(false);
		textContent.addPlugin(plugin);
		textContent.setData("text", "Enter the question content");
		textContent.getMessages().setBlankText("Question content is required");

		comboType.setFieldLabel("Question type");
		comboType.setAllowBlank(false);
		comboType.setEditable(false);
		comboType.addPlugin(plugin);
		comboType.setData("text", "Select the question type");
		comboType.getMessages().setBlankText("Question type is required");

		listResponses.setFieldLabel("Response Options");
		listResponses.setDisplayField("content");
		listResponses.setStore(store);

		HorizontalPanel hp = new HorizontalPanel();
		hp.setStyleAttribute("margin", "10px 180px");
		hp.add(addButton);
		hp.add(deleteButton);

		form.add(textTitle, formData);
		form.add(textContent, formData);
		form.add(comboType, formData);
		form.add(listResponses, formData);
		form.add(hp, formData);

	}

	private void registerHandlers() {
		comboType.addListener(Events.SelectionChange,
				new Listener<BaseEvent>() {
					@Override
					public void handleEvent(BaseEvent be) {
						if (Question.TYPE_OPEN_RESPONSE
								.equalsIgnoreCase(comboType.getSimpleValue())) {
							enableButtons(false);
						} else {
							enableButtons(true);
						}
					}

					void enableButtons(boolean state) {
						addButton.setEnabled(state);
						deleteButton.setEnabled(state);
					}
				});

		listResponses
				.addSelectionChangedListener(new SelectionChangedListener<OptionBeanModel>() {
					@Override
					public void selectionChanged(
							SelectionChangedEvent<OptionBeanModel> event) {
						selectedOption = event.getSelectedItem();
					}
				});

		addButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				// TODO i18n
				showPrompt("Response Option",
						"Please enter the option content", null);
			}
		});

		deleteButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				if (selectedOption != null) {
					deleteOption((OptionBeanModel) selectedOption);
				}
			}
		});
	}

	@Override
	public String getContent() {
		return textContent.getValue();
	}

	@Override
	public void setContent(String content) {
		textContent.setValue(content);
	}

	@Override
	public List<ResponseOption> getOptions() {
		List<ResponseOption> result = new ArrayList<ResponseOption>();
		int count = store.getCount();
		for (int i = 0; i < count; i++) {
			ResponseOption option = new ResponseOption();
			option.setContent(store.getAt(i).getContent());
			result.add(option);
			System.out.println("Option --> " + option.getContent());
		}
		return result;
	}

	@Override
	public void setOptions(List<ResponseOption> options) {
		for (ResponseOption responseOption : options) {
			OptionBeanModel option = new OptionBeanModel();
			option.setId(responseOption.getId());
			option.setContent(responseOption.getContent());
			store.add(option);
		}
	}

	@Override
	public String getQuestionTitle() {
		return textTitle.getValue();
	}

	@Override
	public void setQuestionTitle(String title) {
		textTitle.setValue(title);
	}

	@Override
	public String getType() {
		return comboType.getSimpleValue();
	}

	@Override
	public void setType(String type) {
		comboType.setSimpleValue(type);
	}

	@Override
	public void setTypes(List<String> types) {
		for (String type : types) {
			comboType.add(type);
		}
	}

	public void showPrompt(String title, String msg, String text) {
		MessageBox box = MessageBox.prompt(title, msg);
		box.getTextBox().setValue((text == null) ? "" : text);
		box.addCallback(new Listener<MessageBoxEvent>() {
			@Override
			public void handleEvent(MessageBoxEvent be) {
				if ("OK".equalsIgnoreCase(be.getButtonClicked().getText())) {
					addOption(null, be.getValue());
				}
			}
		});
	}

	public void addOption(String id, String text) {
		OptionBeanModel option = new OptionBeanModel();
		option.setId(id);
		option.setContent(text);
		store.add(option);
	}

	public void deleteOption(OptionBeanModel option) {
		int index = store.indexOf(option);
		store.remove(index);
	}

}
